﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;

namespace Ebada.Modules.Document
{
    public partial class frmDocumentbookEdit2 : FormBase, IPopupFormEdit 
    {
        public IList<Documents> list;
        public frmDocumentbookEdit2()
        {
            InitializeComponent();
        }

        private void frmDocumentbookEdit_Load(object sender, EventArgs e)
        {
            UCDocumentsRight uc = new UCDocumentsRight();
            uc.Dock = DockStyle.Fill;
            uc.initColumns2();
            uc.hidebutton2();
            uc.List = list;
            splitContainerControl1.Panel2.Controls.Add(uc);
            Add_bookID();
        }
        #region IPopupFormEdit Members
        private Documents rowData = null;
        private void Add_bookID()
        {
            IList<Documents> tempdoclist = MainHelper.PlatformSqlMap.GetList<Documents>(" where DocBookID!=''");
            Hashtable temphstable = new Hashtable();
            for (int i = 0; i < tempdoclist.Count; i++)
            {
                if (!temphstable.Contains(tempdoclist[i].DocBookID))
                {
                    temphstable.Add(tempdoclist[i].DocBookID.ToString(), tempdoclist[i].DocBookName.ToString());
                }
            }
            DataTable temptable = new DataTable();
            temptable.Columns.Add("DocBookID");
            temptable.Columns.Add("DocBookName");
            foreach (string key in temphstable.Keys)
            {
                DataRow temprow = temptable.NewRow();
                temprow["DocBookID"] = key;
                temprow["DocBookName"] = temphstable[key].ToString();
                temptable.Rows.Add(temprow);
            }
            txtBookName.Properties.Columns.Clear();
            txtBookName.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
         
            txtBookName.Properties.DataSource = temptable;
            txtBookName.Properties.DisplayMember = "DocBookName";
            txtBookName.Properties.ValueMember = "DocBookID";
            txtBookName.Properties.NullText = "请选择";
            //txtBookName.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            ////new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DocBookName", "ID", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            //new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DocBookName", "册子名称")});
            //txtBookName.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            //new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DocBookName", "册子名称")});
          
            txtBookName.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DocBookID", "册子编号"),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DocBookName", "册子名称")});
               }
        public object RowData
        {
            get
            {
                return rowData;
            }
            set
            {
                if (value == null) return;
                if (rowData == null)
                {
                    this.rowData = value as Documents;
                    //dataBind();
                    //InitComboBoxData();
                }
                else
                {
                    ConvertHelper.CopyTo<Documents>(value as Documents, rowData);
                }
            }
        }

        #endregion

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (txtBookName.Text == "")
            {
                MessageBox.Show("册子编号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtBookName.Focus();
                return;
            }
           
            if (txtBookID.Text == "")
            {
                MessageBox.Show("册子名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtBookID.Focus();
                return;
            }

            for (int i = 0; i < list.Count;i++ )
            {
                list[i].DocBookID = txtBookName.Text;
                list[i].DocBookName = txtBookID.Text;
                MainHelper.GetSqlMap<Documents>().Update<Documents>(list[i]);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtBookName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                frmDocumentBookSeletr frm = new frmDocumentBookSeletr();

                if (frm.ShowDialog() == DialogResult.OK)
                {
                    txtBookName.EditValue = frm.DocBookID;
                }
            }
        }

        private void txtBookName_EditValueChanged(object sender, EventArgs e)
        {
            txtBookID.EditValue = txtBookName.EditValue;
            IList<Documents> tempdoclist = MainHelper.PlatformSqlMap.GetList<Documents>(" where DocBookID='" + txtBookID.EditValue.ToString() + "'");
            int m = tempdoclist.Count;
            m++;
            for (int i = 0; i < list.Count; i++)
            {
                list[i].DocBookSeq = m++;
                tempdoclist.Add(list[i]);
            }
           
            foreach (Control tempcol in splitContainerControl1.Panel2.Controls)
            {
                splitContainerControl1.Panel2.Controls.Remove(tempcol);
            }
            UCDocumentsRight uc = new UCDocumentsRight();
            uc.Dock = DockStyle.Fill;
            uc.initColumns2();
            uc.hidebutton2();
            uc.List = tempdoclist;
            splitContainerControl1.Panel2.Controls.Add(uc);
          
           
        }
    }
}
